package bud.listener;

import java.util.Map;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import bud.vo.LocationDTO;
import bud.vo.MemberDTO;

/**
 * Application Lifecycle Listener implementation class MySessionAttributeListener
 *
 */

public class MySessionAttributeListener implements HttpSessionAttributeListener {

    /**
     * Default constructor. 
     */
    public MySessionAttributeListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent arg0) {
        String attrName = arg0.getName();
        if("memberSession".equals(attrName)){
        	MemberDTO member = (MemberDTO)arg0.getValue();
        	Map<Integer, LocationDTO> userLocation = (Map<Integer, LocationDTO>) arg0.getSession().getServletContext().getAttribute("userLocation");
        	userLocation.remove(member.getMem_no());
        }  
        
    }

	/**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent arg0) {
        String attrName = arg0.getName();
        if("memberSession".equals(attrName)){
        	Map<Integer, LocationDTO> userLocation = (Map<Integer, LocationDTO>) arg0.getSession().getServletContext().getAttribute("userLocation");
        	MemberDTO member = (MemberDTO)arg0.getValue();
        	LocationDTO location = new LocationDTO();
        	
        	userLocation.put(member.getMem_no(), location);
        }
    }

	/**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub
    }
	
}
